package com.view;

import com.clientservice.FileClientService;
import com.clientservice.MessageClientService;
import com.clientservice.UserClientService;
import com.utils.Utility;
import org.junit.Test;

import java.util.Scanner;

/**
 * @author wty
 * @date 2022/10/25 10:59
 * 主菜单
 */
@SuppressWarnings({"all"})
public class QQView {
    private boolean loop = true; // 控制是否加载主菜单
    private String key = ""; // 用户键盘输入
    // 用于登录服务器
    private UserClientService userClientService = new UserClientService();
    private MessageClientService messageClientService = new MessageClientService();
    private FileClientService fileClientService = new FileClientService();

    public static void main(String[] args) throws Exception {
        new QQView().mainMenu();
    }

    @Test
    public void mainMenu() throws Exception {

        do {
            System.out.println("=====欢迎登录网络即时通信系统=====");
            System.out.println("1 登录系统");
            System.out.println("9 退出系统");
            System.out.println("请输入你的选择:");

            // 读取键盘输入的指定长度的字符串
            key = Utility.readString(1);

            switch (key) {
                case "1":
                    System.out.println("请输入用户名:");
                    String userID = Utility.readString(50);
                    System.out.println("请输入密码:");
                    String password = Utility.readString(50);


                    // 这里编写一个类UserClientService 用于登录和注册
                    // 拿到用户名和密码后，需要到服务端去验证用户和密码是否正确
                    if (userClientService.checkUser(userID, password)) {

                        System.out.println("=====欢迎" + userID + "用户,登录成功!=====");
                        // 进入二级菜单
                        while (loop) {
                            System.out.println("=====网络通信系统二级菜单: " + userID + "=====");
                            System.out.println("1.显示在线用户列表");
                            System.out.println("2.群发消息");
                            System.out.println("3.私聊消息");
                            System.out.println("4.发送文件");
                            System.out.println("9.退出系统");
                            System.out.println("请输入你的选择:");
                            key = Utility.readString(1);

                            switch (key) {
                                case "1":
                                    System.out.println("-----显示在线用户列表-----");
                                    // 这里写一个方法，获取用户在线列表
                                    userClientService.onlineFriendList();
                                    break;
                                case "2":
                                    System.out.println("-----群发消息-----");
                                    System.out.println("请输入群聊消息:");
                                    Scanner scanner1 = new Scanner(System.in);
                                    String chatMessages = scanner1.next();
                                    messageClientService.sendMessagetoEveryOne(chatMessages, userID);

                                    break;
                                case "3":
                                    System.out.println("-----私聊消息-----");
                                    System.out.println("请输入你想私聊的对象(在线)<如果对方离线将会离线发送>:");
                                    Scanner scanner = new Scanner(System.in);
                                    String receiver = scanner.next();
                                    System.out.println("请输入想说的话:");
                                    Scanner scanner2 = new Scanner(System.in);
                                    String chatMessage = scanner2.next();
                                    //System.out.println(userID+"对"+receiver+"说：" + chatMessage);
                                    messageClientService.sendMessagetoOne(chatMessage, receiver, userID);

                                    break;
                                case "4":
                                    System.out.println("-----发送文件-----");
                                    System.out.println("请输入想发送文件的用户(在线)<如果对方离线将会离线发送>");
                                    String receiverUser = Utility.readString(100);
                                    System.out.println("请输入想发送文件完整路径(样例：E:\\1\\1.mp4)");
                                    String path = Utility.readString(200);
                                    System.out.println("请输入想发送文件到对方的路径(样例:E:\\2\\2.mp4)");
                                    String targetPath = Utility.readString(200);

                                    fileClientService.sendFile(userID,receiverUser,path,targetPath);

                                    break;
                                case "9":
                                    System.out.println("您退出了系统，感谢使用");
                                    loop = false; // 仅仅只退出了主线程
                                    // 调用方法关闭子线程
                                    userClientService.logout();
                                    break;
                                default:
                                    break;
                            }

                        }
                        break;
                    } else {
                        System.out.println("账号和密码错误，请重新输入");
                        break;
                    }

                case "9":
                    System.out.println("程序已退出");
                    loop = false;
                    break;
                default:
                    System.out.println("输入错误，请重新输入你的选择");
            } // switch
        } while (!"9".equals(key) && loop);


    } // mainMenu
} // class QQView
